package com.example.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.example.myapplication.model.MessageAdapter;
import com.example.myapplication.model.MessageModel;

import java.util.ArrayList;
import java.util.List;

public class SmartChatActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private EditText etMessage;
    private ImageButton btnSendMessage;
    private List<MessageModel> messageList;
    private MessageAdapter adapter;
    private Handler handler = new Handler();
    private int dotCount = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smartchat);

        recyclerView = findViewById(R.id.recyclerView);
        etMessage = findViewById(R.id.etMessage);
        btnSendMessage = findViewById(R.id.btnSendMessage);

        if (!Python.isStarted()){
            Python.start(new AndroidPlatform(this));
        }

        messageList = new ArrayList<>();
        adapter = new MessageAdapter(messageList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        ImageView imgBack = findViewById(R.id.imgBack);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnSendMessage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String messageText = etMessage.getText().toString().trim();
                if (!messageText.isEmpty()) {
                    MessageModel newMessage = new MessageModel(messageText, true); // Your message, so it's you
                    messageList.add(newMessage);
                    adapter.notifyItemInserted(messageList.size() - 1);
                    recyclerView.smoothScrollToPosition(messageList.size() - 1);
                    etMessage.setText("");
                    etMessage.setEnabled(false);

                    dotCount = 0;
                    handler.post(updateDotText);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Python py = Python.getInstance();
                            PyObject pyObj = py.getModule("smartchat");
                            final PyObject result = pyObj.callAttr("main", messageText);
                            final String messageText = result.toString();

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    stopWaitingAnimation();
                                    MessageModel newMessage = new MessageModel(messageText, false); // AI message, so not you
                                    messageList.add(newMessage);
                                    adapter.notifyItemInserted(messageList.size() - 1);
                                    recyclerView.smoothScrollToPosition(messageList.size() - 1);
                                    etMessage.setEnabled(true);
                                    etMessage.setHint("请输入");
                                }
                            });
                        }
                    }).start();
                }
            }
        });
    }

    public void startWaitingAnimation(View view) {
        dotCount = 0;
        handler.post(updateDotText);
    }

    private final Runnable updateDotText = new Runnable() {
        @Override
        public void run() {
            StringBuilder dots = new StringBuilder();
            dots.append("请稍后");
            for (int i = 0; i < dotCount; i++) {
                dots.append(".");
            }
            etMessage.setHint(dots.toString());

            dotCount = (dotCount + 1) % 4;

            handler.postDelayed(this, 500);
        }
    };

    private void stopWaitingAnimation() {
        handler.removeCallbacks(updateDotText);
    }
}
